package com.wfw.feichat.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author: wfw
 * @date: 2019-05-21 16:31
 */
@Component
@Slf4j
public class WsServer {

    private EventLoopGroup parentGroup;
    private EventLoopGroup childGroup;
    private ServerBootstrap serverBootstrap;
    private ChannelFuture future;

    public WsServer() {
        parentGroup = new NioEventLoopGroup();
        childGroup = new NioEventLoopGroup();
        serverBootstrap = new ServerBootstrap();

        serverBootstrap.group(parentGroup, childGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new WsServerIntializer());
    }

    public static WsServer getInstance() {
        return SingletonWsServer.instance;
    }

    public void start() {
        this.future = serverBootstrap.bind(8088);
        log.error("netty websocket server 启动完毕，端口为8088");
    }

    private static class SingletonWsServer {
        static final WsServer instance = new WsServer();
    }

}
